#include "frame.h"
#include <iostream>
#include "asynsocketclient.h"
#include "communication/session.h"
//const u_char Frame::begin1=0x42;
//const u_char Frame::begin2=0x53;
//const u_char Frame::version=0x03;
bool Frame::ParseRecvData(const size_t bytes_transferred, boost::shared_ptr<AsynSocketClient> client)
{
    bool flag=false;

    for (size_t i=0;i<bytes_transferred;i++)
    {
        switch (input_index)
        {
        case 0:
            if(recv_data[i]==SunlinkProtocal::kSunlinkHeader1)
            {
                input_index++;
            }
            break;
        case 1:
            if(recv_data[i]==SunlinkProtocal::kSunlinkHeader2)
            {
                input_index++;
            }
            else
            {
                if(recv_data[i]==SunlinkProtocal::kSunlinkHeader1)
                    input_index=1;
                else
                    input_index=0;
            }
            break;
        case 2:
            if(recv_data[i]==SunlinkProtocal::kSunlinkVersion)
            {
                input_index++;
            }
            else
            {
                if(recv_data[i]==SunlinkProtocal::kSunlinkHeader1)
                    input_index=1;
                else
                    input_index=0;
            }
            break;
        case 3:case 4:
            data[input_index]=recv_data[i];
            input_index++;
            break;
        default:
            {
                u_int16_t usr_data_length=data[3]+data[4]*(2^8);
                if(usr_data_length>input_index-5+bytes_transferred-i)
                {
                    memcpy(data+input_index,recv_data+i,bytes_transferred-i);
                    input_index=input_index+bytes_transferred-i;
                    i=bytes_transferred;
                }
                else {
                    memcpy(data+input_index,recv_data+i,usr_data_length-(input_index-5));
                    i+=usr_data_length-(input_index-4);
                    input_index=0;

                    u_int16_t cal_crc=CalCRC16(data,usr_data_length+3);
                    u_int16_t recv_crc=data[usr_data_length+5-2]+data[usr_data_length+5-1]*256;
                    //std::cout<<"cal_crc:"<<cal_crc<<" recv_crc:"<<recv_crc<<std::endl;
                    if(cal_crc==recv_crc)
                    {
//                            memcpy(usr_data,data+5,usr_data_length);
//                            usr_data_len=usr_data_length;
                        cout<<"full frame"<<endl;
                        client->HandleDataCb(data,usr_data_length+5);
                        flag=true;

//                            std::cout<<"got all usr data"<<std::endl;
//                            for(size_t k = 5; k < usr_data_length+5; k++)
//                                std::cout << (int)data[k]<<" ";
//                            std::cout << std::endl;
                    }
                    else {

                        std::cout<<"wrong crc, frame dropped"<<std::endl;
                    }
                }
            }
            break;
        }
    }
    return flag;
}
